/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.mail.test.base;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Maps;
import info.peigen.hotpot.common.core.util.IdUtil;
import info.peigen.hotpot.common.service.base.test.ServiceTestBase;
import info.peigen.hotpot.common.service.base.test.mock.ServiceTestMockContextThreadLocal;
import info.peigen.hotpot.component.mail.MailApplication;
import info.peigen.hotpot.component.mail.domain.MailTemplate;
import lombok.AccessLevel;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.extension.ExtendWith;
import org.noear.solon.test.SolonJUnit5Extension;
import org.noear.solon.test.SolonTest;

import java.util.Map;

/**
 * <b>(MailTestBase)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2023/1/19
 */
@SuppressWarnings("NewClassNamingConvention")
@Slf4j
@SolonTest(MailApplication.class)
@ExtendWith(SolonJUnit5Extension.class)
@FieldDefaults(level = AccessLevel.PROTECTED)
public class MailTestBase extends ServiceTestBase implements MailTestBaseMockConstant {

    ServiceTestMockContextThreadLocal mockContext = new ServiceTestMockContextThreadLocal();

    protected MailTemplate genMailTemplate(String templateId) {
        if (StrUtil.isEmpty(templateId)) {
            templateId = IdUtil.id();
        }

        return MailTemplate.builder().templateId(templateId).context(MAIL_TEMPLATE).templateMap(genTemplateMap())
                .build();
    }

    protected MailTemplate genInvoiceMailTemplate(String templateId) {
        if (StrUtil.isEmpty(templateId)) {
            templateId = IdUtil.id();
        }

        return MailTemplate.builder().templateId(templateId).context(INVOICE_MAIL_TEMPLATE)
                .templateMap(genTemplateMap()).build();
    }

    protected Map<String, Object> genTemplateMap() {
        Map<String, Object> templateMap = Maps.newHashMap();
        templateMap.put("userName", "Peigen");
        templateMap.put("age", "33");
        templateMap.put("level", "3");
        return templateMap;
    }

    public static void main(String[] args) {
        final String[]      context     = {MAIL_TEMPLATE};
        MailTestBase        mailTest    = new MailTestBase();
        Map<String, Object> templateMap = mailTest.genTemplateMap();
        templateMap.forEach((k, v) -> context[0] = StrUtil.replace(context[0], "@" + k, (String) v));
        System.out.println(context[0]);
    }
}